SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

create function [dbo].[validacuil](@cuil varchar(11)) RETURNS bit
AS
BEGIN
Declare @suma numeric(10,2) 
Declare @valido bit

if len(@cuil) <> 11 
set @valido = 1 
else 
set @suma = 0 
set @suma = @suma + convert(int,substring(@cuil,1,1)) * 5 
set @suma = @suma + convert(int,substring(@cuil,2,1)) * 4 
set @suma = @suma + convert(int,substring(@cuil,3,1)) * 3 
set @suma = @suma + convert(int,substring(@cuil,4,1)) * 2 
set @suma = @suma + convert(int,substring(@cuil,5,1)) * 7 
set @suma = @suma + convert(int,substring(@cuil,6,1)) * 6 
set @suma = @suma + convert(int,substring(@cuil,7,1)) * 5 
set @suma = @suma + convert(int,substring(@cuil,8,1)) * 4 
set @suma = @suma + convert(int,substring(@cuil,9,1)) * 3 
set @suma = @suma + convert(int,substring(@cuil,10,1)) * 2 
set @suma = @suma + convert(int,substring(@cuil,11,1)) * 1 

--Reemplazo para la funcion de modulo 
if round(@suma / 11,0) = (@suma / 11) 
set @valido = 1
else 
set @valido = 0
RETURN (@valido) 
end

GO
